package com.wica.view.tab_preference;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.Toast;

import com.wica.facetube.R;

public class TabPreferencesSearch extends TabPreferences {
	
	// *************** Constants ************** //  
    public static final String SEARCH_TAB_PREFERENCES_1 = "list_search_preference";
    public static final String SEARCH_TAB_PREFERENCES_2= "numberOfResults_search_preference";
    public static final int MAX_SEARCH_TAB_PREFERENCES_2=30;

	// *************** Methods **************************** //
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.search_preferences);    
    }// onCreate method    

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		if (key.equals(SEARCH_TAB_PREFERENCES_1)) {
    		Toast.makeText(this, sharedPreferences.getString(key, "relevance")+" results will be shown in next search performed.", Toast.LENGTH_SHORT).show();
    		finish();
		}else if (key.equals(SEARCH_TAB_PREFERENCES_2)) {
        	Integer integer = new Integer(sharedPreferences.getString(key, "0"));
        	int i = integer.intValue();
        	if (i<MAX_SEARCH_TAB_PREFERENCES_2){
        		Toast.makeText(this, sharedPreferences.getString(key, "0")+" results will be shown in next search performed.", Toast.LENGTH_SHORT).show();
        		finish();
        	}else if (i==MAX_SEARCH_TAB_PREFERENCES_2){
        		Toast.makeText(this, "The maximum number of results ("+MAX_SEARCH_TAB_PREFERENCES_2+") will be shown in next search.", Toast.LENGTH_SHORT).show();
        		finish();
        	}else{
        		Editor s = sharedPreferences.edit();        		
        		s.putString(key, ""+MAX_SEARCH_TAB_PREFERENCES_2);        		
        		s.commit();
        	}        
        }		
		
	}// onSharedPreferenceChanged method

}// TabPreferencesSearch class
